/**
 * Bubble.java
 * 
 * @authors: Connor Gordon, Difan Zhao, Kathleen Lan, Patrick Royal
 */

package view.visualComponent;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.util.Random;
import utilities.Calculator;


/**
 * Bubble represents individual data, the size is proportional
 * to proportion of the data's value to the sum of all data values.
 * 
 * @author difan zhao, kathleen lan
 * 
 */

public class Bubble implements ElementVisual {
    private static final int X_POSITION_OFFSET = 10;
    private static final int Y_POSITION_OFFSET = 6;
    private String myName;
    // abbreviation for name, or in the case of stocks, the stock ticker
    private String myLabel;
    private Shape myShape;
    private int myX;
    private int myY;
    private int myRadius;
    // private BubbleDrawer myBubbleDrawer;
    private Calculator myCalculator = new Calculator();

    private Paint myShapeColor;
    private Paint myLabelColor = Color.black;
    private Paint[] myColorOptions = {
        Color.BLUE, Color.CYAN, Color.GRAY,
        Color.GREEN, Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE,
        Color.PINK, Color.RED, Color.WHITE, Color.YELLOW
    };
    private Random myGenerator = new Random();

    /**
     * Constructor for bubble.
     * 
     * @param x x-coordinate
     * @param y y-coordinate
     * @param radius radius of the bubble
     * @param name name of the bubble
     * @param label label of the bubble
     */
    public Bubble (int x, int y, int radius, String name, String label) {
        myX = x - radius;
        myY = y - radius;
        myRadius = radius;
        myShape = new Ellipse2D.Double(myX, myY, 2 * radius, 2 * radius);
        myName = name;
        myLabel = label;
        setColor();
    }

    /**
     * Sets the color of the Bubble randomly.
     */
    private void setColor () {
        int colorNum = myGenerator.nextInt(myX + myY) % myColorOptions.length;
        myShapeColor = myColorOptions[colorNum];
    }

    /**
     * Determines whether mouse-click event occurred inside the area of a
     * Bubble.
     * 
     * @param x x-coordinate of mouse-click event location
     * @param y y-coordinate of mouse-click event location
     * @return
     */
    public String clickedOn (int x, int y) {
        if (intersectsWithElementVisual(x, y, 0, 0)) { return myName; }
        return null;
    }

    /**
     * Draws this Bubble.
     * 
     * @param g graphics object
     */
    public void paint (Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.draw(myShape);
        g2d.setPaint(myShapeColor);
        g2d.fill(myShape);
        g2d.setPaint(myLabelColor);
        g2d.drawString(myLabel, myX + myRadius - X_POSITION_OFFSET, myY +
                myRadius + Y_POSITION_OFFSET);
    }

    /**
     * Determines whether a Bubble of radius r centered at test point (x,y)
     * intersects with this Bubble. If r=0, determines whether
     * the test point (x,y) is contained within Bubble (i.e. intersects with
     * Bubble).
     * 
     * @param x x-coordinate of center of test point
     * @param y y-coordinate of center of test point
     * @param r radius of test Bubble
     * @param r2 radius of other Bubble
     * @return
     */
    public boolean intersectsWithElementVisual (int x, int y, int r, int r2) {
        return myCalculator.distanceBetween(x, y, myX + myRadius, myY +
                myRadius) < (r + myRadius);
    }

    /**
     * Returns radius of this Bubble.
     * 
     * @return
     */
    public int getRadius () {
        return myRadius;
    }

    /**
     * Returns label of this Bubble.
     * 
     * @return
     */
    public String getLabel () {
        return myLabel;
    }

}
